﻿using NHibernate;
using SmartRS.com.vn.smartrs.dao;
using SmartRS.com.vn.smartrs.domain;
using SmartRS.com.vn.smartrs.service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SmartRS.com.vn.smartrs.service
{
    public class FoodService : BaseService<Food>
    {
        private static FoodService _foodService = new FoodService();

        private FoodService()
        { }

        public static FoodService getInstance()
        {
            return _foodService;
        }

        public bool deleteListFood(IList<Food> list)
        {
            using (ISession session = NHibernateHelper.OpenSession())
            {
                using (ITransaction transaction = session.BeginTransaction())
                {
                    try
                    {
                        foreach(Food f in list)
                        {
                            session.Delete(f);
                        }
                        
                        transaction.Commit();
                        return true;
                    }
                    catch (Exception ex)
                    {
                        if (transaction.WasCommitted)
                        {
                            transaction.Rollback();
                            return false;
                        }
                        throw new Exception("Delete happened error: " + ex.Message);
                    }

                }
            }
        }

    }
}
